package com.example.testi18n.pdf;

/**
 * @author xiaobendan
 * @version v1.0.0
 * @className ProducePDF
 * @package com.example.testi18n.pdf
 * @description 生成PDF票据
 * @date 2022-09-30 08:39
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.lang3.time.StopWatch;


public class ProducePDF {

    Document document = null;// 建立一个Document对象
    private static Font headFont;
    private static Font keyFont;
    private static Font textfont_H;
    private static Font textfont_B;
    int maxWidth = 520;

    static {
        BaseFont bfChinese_H;
        try {
            /**
             * 新建一个字体,iText的方法 STSongStd-Light 是字体，在iTextAsian.jar 中以property为后缀
             * UniGB-UCS2-H 是编码，在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版， 相应的 V 代表竖版
             */
            bfChinese_H = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            headFont = new Font(bfChinese_H, 10, Font.NORMAL);
            keyFont = new Font(bfChinese_H, 18, Font.BOLD);
            textfont_H = new Font(bfChinese_H, 10, Font.NORMAL);
            textfont_B = new Font(bfChinese_H, 12, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置页面属性
     *
     * @param file
     */
    public ProducePDF(File file) {

        //自定义纸张
        Rectangle rectPageSize = new Rectangle(520, 580);

        // 定义A4页面大小
        // Rectangle rectPageSize = new Rectangle(PageSize.A4);
        // 加上这句可以实现页面的横置
        rectPageSize = rectPageSize.rotate();
        document = new Document(rectPageSize, 20, 20, 20, 40);

        try {
            PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 建表格(以列的数量建)
     */
    public PdfPTable createTable(int colNumber) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            //table.setTotalWidth(maxWidth);
            //table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 建表格(以列的宽度比建)
     */
    public PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            //table.setTotalWidth(maxWidth);
            //table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }


    /**
     * 表格中单元格
     */
    public PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 表格中单元格
     */
    public PdfPCell createCell(String value, Font font, int align_v, int align_h, int colspan, int rowspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(align_v);
        cell.setHorizontalAlignment(align_h);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }


    /**
     * 无边框单元格
     */
    public PdfPCell createCellNoBorder(String value, Font font, int align_v, int align_h, int colspan, int rowspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(align_v);
        cell.setHorizontalAlignment(align_h);
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setBorder(0);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 建短语
     */
    public Phrase createPhrase(String value, Font font) {
        Phrase phrase = new Phrase();
        phrase.add(value);
        phrase.setFont(font);
        return phrase;
    }

    /**
     * 建段落
     */
    public Paragraph createParagraph(String value, Font font, int align) {
        Paragraph paragraph = new Paragraph();
        paragraph.add(new Phrase(value, font));
        paragraph.setAlignment(align);
        return paragraph;
    }


    public void generatePDF() throws Exception {

        for (int i = 0; i < 1; i++) {



            // 页头信息
            document.add(createParagraph("收      据", keyFont, Element.ALIGN_CENTER));
            // 直线
            document.add(createParagraph("______________", headFont, Element.ALIGN_CENTER));
            document.add(createParagraph("______________", headFont, Element.ALIGN_CENTER));

            document.add(createParagraph("NoXD201602000003", headFont, Element.ALIGN_RIGHT));
            document.add(createParagraph("入账日期：    年   月   日", headFont, Element.ALIGN_CENTER));

//            document.add(createParagraph("内", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("部", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("使", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("用", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("   ", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("不", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("作", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("发", headFont, Element.ALIGN_LEFT));
//            document.add(createParagraph("票", headFont, Element.ALIGN_LEFT));

//            document.add(createParagraph("第", headFont, Element.ALIGN_RIGHT));
//            document.add(createParagraph("一", headFont, Element.ALIGN_RIGHT));
//            document.add(createParagraph("联", headFont, Element.ALIGN_RIGHT));
//            document.add(createParagraph("：", headFont, Element.ALIGN_RIGHT));
//            document.add(createParagraph("存", headFont, Element.ALIGN_RIGHT));
//            document.add(createParagraph("根", headFont, Element.ALIGN_RIGHT));

            // 表格信息
            float[] widths = {1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f};
            PdfPTable table = createTable(widths);

            // 第一行
            table.addCell(createCellNoBorder("内", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("收         到", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("____________________________________________________", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_RIGHT, 6, 1));
            table.addCell(createCellNoBorder("", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));

            // 第二行
            table.addCell(createCellNoBorder("部", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("____________________________________________________", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_RIGHT, 8, 1));
            table.addCell(createCellNoBorder("第", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));


            // 第三行
            table.addCell(createCellNoBorder("使", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("____________________________________________________", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_RIGHT, 8, 1));
            table.addCell(createCellNoBorder("一", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));


            // 第四行
            table.addCell(createCellNoBorder("用", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("金额(大写)", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("  佰     拾    万    仟    佰    拾    元    角    分", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, 6, 1));
            table.addCell(createCellNoBorder("联", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));

            // 第五行
            table.addCell(createCellNoBorder("不", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("备         注：", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("¥:", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 6, 1));
            table.addCell(createCellNoBorder(":", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));

            // 第六行
            table.addCell(createCellNoBorder("作", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,1,1));
            table.addCell(createCell("发据单位(盖章):", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("会计:", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("出纳:", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCell("经手人:", textfont_B, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, 2, 1));
            table.addCell(createCellNoBorder("存", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));

            // 第七行
            table.addCell(createCellNoBorder("发", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,9,1));
            table.addCell(createCellNoBorder("根", headFont, Element.ALIGN_LEFT, Element.ALIGN_RIGHT,1,1));

            // 第八行
            table.addCell(createCellNoBorder("票", headFont, Element.ALIGN_LEFT, Element.ALIGN_LEFT,10,1));


            // 添加图片
            Image image = Image.getInstance("/Users/xiaobendan/Desktop/666.jpg");
            image.setAbsolutePosition(180, 312);

            // 依照比例缩放
            image.scalePercent(0.5F);
            // 旋转(180-参数，即为顺时针旋转的度数)
            image.setInitialRotation(120);



            document.add(table);
            document.add(image);
            document.newPage();
        }
        document.close();
    }

    public static void main(String[] args) throws Exception {
        File file = new File("/Users/xiaobendan/Desktop/ticket4.pdf");
        file.createNewFile();
        new ProducePDF(file).generatePDF();

//        FileInputStream fileInputStream = new FileInputStream(file);
//        FileOutputStream fileOutputStream = new FileOutputStream("/Users/xiaobendan/Desktop/bageyalu.pdf");
//        //定义整型变量len,记住每次读取的一个字节
//        int len;
//        StopWatch stopWatch = StopWatch.createStarted();
//        // 读取一个字节，并判断是否读到文件末尾
//        while((len=fileInputStream.read())!=-1) {
//            // 将读到的字节写入文件
//            fileOutputStream.write(len);
//        }
//        System.out.println("文件拷贝所消耗的时间是:"+ stopWatch.getTime() +"毫秒");
//        fileInputStream.close();
//        fileOutputStream.close();


        System.out.println("PDF生产完成");
    }
}
